package com.zygame.sdk.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;

public class ImageSelector {
	
	/**
	 * 通过Bitmap设置
	 * @param context
	 * @param normalBmp
	 * @param pressBmp
	 * @return
	 */
	public static StateListDrawable newSelector(Context context,Bitmap normalBmp,Bitmap pressBmp){
		
		StateListDrawable bg = new StateListDrawable();
		BitmapDrawable normal = new BitmapDrawable(context.getResources(),normalBmp);
		BitmapDrawable pressed = new BitmapDrawable(context.getResources(),pressBmp);
		bg.addState(new int[] { android.R.attr.state_pressed,android.R.attr.state_enabled}, pressed);
		bg.addState(new int[] {}, normal);
		return bg;
	}
	
	public static StateListDrawable newSelector2(Context context,Drawable normalBmp,Drawable pressBmp){
		
		StateListDrawable bg = new StateListDrawable();
//		BitmapDrawable normal = new BitmapDrawable(context.getResources(),normalBmp);
//		BitmapDrawable pressed = new BitmapDrawable(context.getResources(),pressBmp);
		bg.addState(new int[] { android.R.attr.state_pressed,android.R.attr.state_enabled}, pressBmp);
		bg.addState(new int[] {}, normalBmp);
		return bg;
	}
	
	/**
	 * 通过id设置
	 * @param context
	 * @param idNormal
	 * @param idPressed
	 * @return
	 */
	public static StateListDrawable newSelector(Context context, int idNormal,int idPressed) {
		StateListDrawable bg = new StateListDrawable();
		Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
		Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
		bg.addState(new int[] { android.R.attr.state_pressed,android.R.attr.state_enabled,android.R.attr.state_checked}, pressed);
		bg.addState(new int[] {}, normal);
		return bg;
	}
    
	/**
	 * 通过Bitmap，设置RadioButton
	 * @param context
	 * @param normalBmp
	 * @param checkedBmp
	 * @return
	 */
	public static StateListDrawable radioSelector(Context context,Bitmap normalBmp,Bitmap checkedBmp){
		
		StateListDrawable bg = new StateListDrawable();
		BitmapDrawable normal = new BitmapDrawable(context.getResources(),normalBmp);
		BitmapDrawable checked = new BitmapDrawable(context.getResources(),checkedBmp);
		bg.addState(new int[] {android.R.attr.state_checked}, checked);
		
		bg.addState(new int[] {}, normal);
		return bg;
	}
	
	/**
	 * 通过id，设置RadioButton
	 * @param context
	 * @param normalBmp
	 * @param checkedBmp
	 * @return
	 */
	public static StateListDrawable radioSelector(Context context,int normaId,int checkedId){
		
		StateListDrawable bg = new StateListDrawable();
		Drawable normal = normaId == -1 ? null : context.getResources().getDrawable(normaId);
		Drawable checked = checkedId == -1 ? null : context.getResources().getDrawable(checkedId);
		bg.addState(new int[] {android.R.attr.state_checked}, checked);
		
		bg.addState(new int[] {}, normal);
		return bg;
	}
}



